﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace KjsyModUpdaterForWindows
{
    class Program
    {
        public const string URL = @"http://mmus-1254073414.cossh.myqcloud.com/";

        public static BaseModList baseModList = null;
        public static OptionalModList optionalModList = null;
        private static LocalModList localModList = null;

        static void Main(string[] args)
        {
            /**
             * 先在本地获取list
             * 再去服务端拉取list
             * 然后再去对比
             * 本地多出来的先暂不做处理
             * 服务器多的加入到List里面进行统一下载
             * 可选mod手动确认下载
             */

            List<ModInfo> list = new List<ModInfo>();

            Console.WriteLine("获取本地mod列表中...");

            foreach(var i in Directory.GetFiles(".\\.minecraft\\mods"))
                list.Add(new ModInfo
                {
                    FileName = Path.GetFileName(i),
                    HashCode = Tools.GetHashCode(new FileStream(i, FileMode.Open))
                });

            Console.WriteLine("获取更新信息...");
            DataAnalysis da = new DataAnalysis();
            da.Analysis(Tools.Download("Kjsy-1.7.10/", "list"));

            var baselist = baseModList.Mod.Except(localModList.Mod, new NormalEqualityComparer()).ToList();
            var optilist = optionalModList.Mod.Except(localModList.Mod, new NormalEqualityComparer()).ToList();
            var removelist = localModList.Mod.Except(baseModList.Mod, new RemoveEqualityComparer()).ToList();
            foreach (var i in localModList.Mod.Except(optionalModList.Mod, new RemoveEqualityComparer()).ToList())
                removelist.Add(i);

            Console.Write("更新信息获取完成.");
            if (baselist.Count == 0)
            {
                Console.WriteLine("mod已为最新, 无需更新. 键入任意键退出.");
                Console.ReadKey();
                return;
            }
            Console.WriteLine("发现新内容. 开始更新.");
            foreach(var i in baselist)
            {
                Console.WriteLine("当前操作: 下载");
                Console.WriteLine("目标文件: " + i.FileName);
                FileStream fs = new FileStream(".\\.minecraft\\mods\\" + i.FileName, FileMode.Create);
                Tools.Download("Kjsy-1.7.10/", i.FileName).CopyTo(fs);
                fs.Flush();
                fs.Close();
                Console.WriteLine("操作成功完成.");
            }
            foreach(var i in removelist)
            {
                Console.WriteLine("当前操作: 删除");
                Console.WriteLine("目标文件: " + i.FileName);
                try
                {
                    File.Delete(".\\.minecraft\\mods\\" + i);
                }catch(Exception e)
                {
                    Console.WriteLine("出现异常.跳过该文件.");
                    continue;
                }
                Console.WriteLine("操作成功完成.");
            }

            if(optilist.Count != 0)
            {
                Console.WriteLine("检索到有扩展mod. 按Y开始安装, 按N跳过.");
                Console.WriteLine("扩展mod列表: ");
                foreach (var i in optilist) Console.WriteLine("  " + i.FileName);

                var choose = Console.ReadKey(false);
                if (choose.KeyChar == 'y' || choose.KeyChar == 'Y')
                {
                    Console.WriteLine();
                    Console.WriteLine("开始下载扩展mod.");
                    foreach (var i in optilist)
                    {
                        Console.WriteLine("当前操作: 下载");
                        Console.WriteLine("目标文件: " + i.FileName);
                        FileStream fs = new FileStream(".\\.minecraft\\mods\\" + i.FileName, FileMode.Create);
                        Tools.Download("Kjsy-1.7.10/", i.FileName).CopyTo(fs);
                        fs.Flush();
                        fs.Close();
                        Console.WriteLine("操作成功完成.");
                    }
                }
            }

            Console.WriteLine("更新完成. 按任意键退出更新工具.");
            Console.ReadKey();
        }
    }

    public class NormalEqualityComparer : IEqualityComparer<ModInfo>
    {
        public bool Equals(ModInfo x, ModInfo y)
        {
            if (Object.ReferenceEquals(x, y)) return true;
            return x.FileName == y.FileName;
        }

        public int GetHashCode(ModInfo obj)
        {
            return obj.GetHashCode();
        }
    }

    public class RemoveEqualityComparer : IEqualityComparer<ModInfo>
    {
        public bool Equals(ModInfo x, ModInfo y)
        {
            if (Object.ReferenceEquals(x, y)) return true;
            return (x.FileName == y.FileName) && (x.HashCode == y.HashCode);
        }

        public int GetHashCode(ModInfo obj)
        {
            return obj.GetHashCode();
        }
    }
}
